﻿using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Documents;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Collections;
using System.Data;
using System.Diagnostics;
using Data;
using System;
namespace Data
{
   public class Model3D // Doi tuong chua cac luoi tam giac.
    {
      public List<Mesh3D> list_mesh3D ;
       public Model3D() 
       {
           list_mesh3D = new List<Mesh3D>();
       }
      public int GetModelNumber()
      {
          return list_mesh3D.Count;
      }
      public Mesh3D GetModel(int i)
      {
          return list_mesh3D[i];
      }
      //public Model3DGroup Create_solid()
      // {

      //     Model3DGroup mdgroup = new Model3DGroup();
      //     for (int i = 0; i < list_mesh3D.Count; i++)
      //     {
      //         mdgroup.Children.Add(list_mesh3D[i].CreateMesh(Colors.Aquamarine));
      //     }

      //     return mdgroup;
      //}
      //public Model3DGroup Create_Solid(int i)
      //{

      //    return list_mesh3D[i].CreateMesh(Colors.Aquamarine);
      //}
      public ModelVisual3D Create_Model3D(int i)// tao doi tuong modelvisual3d tu thanh phan i
      {
          
          return list_mesh3D[i].CreateModel3D(Colors.Aquamarine);
          
      }
      public Point3D GetCenter()
      {
          
          Double x, y, z;
          int i = 0;
          x = y = z = 0;
          for (i = 0; i < list_mesh3D.Count; i++)
          {
              x = x + list_mesh3D[i].getCenter().X;
              y = y + list_mesh3D[i].getCenter().Y;
              z = z + list_mesh3D[i].getCenter().Z;
          }
          
          if (i == 0)
              return new Point3D(x,y,z);
          x =Math.Round( x / i,2); y =Math.Round( y / i,2); z =Math.Round( z / i,2);
          return new Point3D(x,y,z);
              
      }
      public Double Zmax()
      {
          Double temp = 0;
          int i = 0;
          temp = list_mesh3D[0].Zmax();
          for (i = 0; i < list_mesh3D.Count; i++)
          {
              if (list_mesh3D[i].Zmax() > temp)
                  temp = list_mesh3D[i].Zmax();
          }
          Debug.WriteLine("[Model] Z MAX :"+temp.ToString());
          return temp;
      }
      public Double Zmin()
      {
          Double temp = 0;
          int i = 0;
          temp = list_mesh3D[0].Zmin();
          for (i = 0; i < list_mesh3D.Count; i++)
          {
              if (list_mesh3D[i].Zmin() < temp)
                  temp = list_mesh3D[i].Zmin();
          }
          Debug.WriteLine("[Model] Z MIN :" + temp.ToString());
          return temp;
      }
      public Double Ymax()
      {
          Double temp = 0;
          int i = 0;
          temp = list_mesh3D[0].Ymax();
          for (i = 0; i < list_mesh3D.Count; i++)
          {
              if (list_mesh3D[i].Ymax() > temp)
                  temp = list_mesh3D[i].Ymax();
          }
          Debug.WriteLine("[Model] Y MAX :" + temp.ToString());
          return temp;
      }
      public Double Ymin()
      {
          Double temp = 0;
          int i = 0;
          temp = list_mesh3D[0].Ymin();
          for (i = 0; i < list_mesh3D.Count; i++)
          {
              if (list_mesh3D[i].Ymin() < temp)
                  temp = list_mesh3D[i].Ymin();
          }
          Debug.WriteLine("[Model] Y MIN :" + temp.ToString());
          return temp;
      }
      public Double Xmax()
      {
          Double temp = 0;
          int i = 0;
          temp = list_mesh3D[0].Xmax();
          for (i = 0; i < list_mesh3D.Count; i++)
          {
              if (list_mesh3D[i].Xmax() > temp)
                  temp = list_mesh3D[i].Xmax();
          }
          Debug.WriteLine("[Model] X MAX :" + temp.ToString());
          return temp;
      }
      public Double Xmin()
      {
          Double temp = 0;
          int i = 0;
          temp = list_mesh3D[0].Xmin();
          for (i = 0; i < list_mesh3D.Count; i++)
          {
              if (temp > list_mesh3D[i].Xmin())
                  temp = list_mesh3D[i].Xmin();
          }
          Debug.WriteLine("[Model] X MIN :" + temp.ToString());
          return temp;
      }


    }
}
